package com.yootk.servlet;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;

@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("userid"); // 获取用户名
        String password = req.getParameter("password"); // 获取密码
        // 为便于核心功能实现，本次登录只要密码输入为“yootk”就表示登录成功
        if (!"yootk".equals(password)) { // 密码错误匹配
            req.setAttribute("error", "错误的用户名及密码！"); // 属性设置
            req.getRequestDispatcher("/errors.jsp").forward(req, resp); // 跳转到错误页
        }
        req.getSession().setAttribute("userid", id); // 保存session属性
        resp.sendRedirect("/pages/front/welcome.jsp"); // 跳转
    }
}
